﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using System.Data;
using System.Web.Services;

namespace Anuntiomatic.Server
{
    public partial class SysAssignRightsGroupPage : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadComboSource();
                BindDataGrid();
            }
        }

        void LoadComboSource()
        {
            ddlSysGroup.DataSource = SysGroup.GetAll();
            ddlSysGroup.DataValueField = "GroupID";
            ddlSysGroup.DataTextField = "GroupName";
            ddlSysGroup.DataBind();
        }


        void BindDataGrid()
        {
            try
            {
                int GroupID = int.Parse(ddlSysGroup.SelectedValue);
                DataTable dtSource = SysMenu.GetByGroupID(GroupID);
                grvRights.DataSource = dtSource;
                grvRights.DataBind();
                foreach (GridViewRow row in grvRights.Rows)
                {
                    if (!row.Cells[1].Text.StartsWith("---"))
                    {
                        row.Cells[1].Text = string.Format("<b>{0}</b>", row.Cells[1].Text);
                    }
                    string menuID = row.Cells[0].Text;
                    for (int i = 0; i < dtSource.Columns.Count; i++)
                    {
                        string text = row.Cells[i].Text;
                        if (text.IsBolean())
                        {
                            bool check = bool.Parse(text);
                            row.Cells[i].Attributes.Add("style", "text-align:center");
                            row.Cells[i].Text = string.Format("<input type='checkbox' {0} value='{1}'/>",
                                            check ? "checked='checked'" : "",
                                            string.Format("{0}|{1}|{2}", GroupID, menuID, dtSource.Columns[i].ColumnName));
                        }
                    }
                }
            }
            catch { }
        }

        protected void ddlSysGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        protected void grvRights_RowCreated(object sender, GridViewRowEventArgs e)
        {
            try
            {
                e.Row.Cells[0].Visible = false;
            }
            catch { }
        }

        [WebMethod]
        public static void SetRight(int groupId, int menuId, string roleCode, bool check)
        {
            if (SAuthorize(Role.Write))
            {
                SysGroupRoleMenu.SetRight(groupId, menuId, roleCode, check);
                SInsertChangeLog(string.Format("Phân quyền nhóm người dùng. GroupID:{0}, MenuID: {1}, RoleCode: {2}, HasRole: {3}", groupId, menuId, roleCode, check));
            }
            else
                throw new Exception("Bạn không có quyền thực hiện chức năng này");
        }
    }
}